#include "playscene.h"
#include <QDebug>
#include <QMenu>
#include <QPainter>
#include "mypushbutton.h"
#include <QTimer>
#include <QLabel>
#include <mycoin.h>
#include <dataconfig.h>
#include <QPropertyAnimation>
#include <QSound>
//PlayScene::PlayScene(int num) : QMainWindow(parent)
//{

//}
PlayScene::PlayScene(int levelNum)
{

//    QString str=QString("您选择的是第%1关").arg(levelNum);
//    qDebug()<<str;
    this->levelIndex=levelNum;
    //初始化大小
    this->setFixedSize(400,588);
    this->setWindowIcon(QPixmap(":/Coin0001.png"));
    this->setWindowTitle("fan金币！！！");
    //  菜单栏（QMenuBar）包含诸多菜单（QMenu)，菜单中包含诸多按钮(QAction)
    QMenuBar *bar=this->menuBar();
    this->setMenuBar(bar);
    QMenu *startMenu=new QMenu("开始");
    QAction *quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,this,[=](){this->close();});
    //定义   返回按钮的音乐  点击按钮的音乐   游戏成功的音乐
    QSound *backSound=new QSound(":/BackButtonSound.wav",this);
    QSound *coinFlipSound=new QSound(":/ConFlipSound.wav",this);
    QSound *succeedSound=new QSound(":/LevelWinSound.wav",this);
    //定义返回按钮
    MyPushButton *backBtn=new MyPushButton(":/BackButton.png",":/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());
    //定义返回按钮的信号和槽
    connect(backBtn,&QPushButton::clicked,[=](){
        backSound->play();
        //监听主场景的返回
        QTimer::singleShot(500,this,[=](){emit this->backSig();});
    });
    //设置关卡的标志，
    //使用QFont设置字体
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(15);
    QString str2=QString("Level %1").arg(levelNum);
    QLabel *label=new QLabel(this);
    label->setFont(font);
    label->setText(str2);
    label->move(0,0.9*this->height());
    //设置胜利图片
    QLabel *winLabel=new QLabel(this);
    QPixmap pixWin;
    pixWin.load(":/LevelCompletedDialogBg.png");
    winLabel->setFixedSize(pixWin.width(),pixWin.height());
    winLabel->setPixmap(pixWin);
    winLabel->move((this->width()-pixWin.width())*0.5,-pixWin.height());
    //引入关卡设置的数据库
    dataConfig config1;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            //设置金币的背景图片，使用QLabel工具
            QPixmap pix_bgd(":/BoardNode(1).png");
            QLabel *label_bgd=new QLabel(this);
            label_bgd->setGeometry(0,0,50,50);
            label_bgd->setPixmap(pix_bgd);
            label_bgd->move(this->width()/2-100+i*pix_bgd.width(),200+j*pix_bgd.height());
            //初始化一个vector二维矩阵，用于接收关卡数据库中的数据
            this->v1=config1.mData[levelNum];
            //根据二维矩阵中存储的数值  初始化关卡的初始场景
            MyCoin *coin=NULL;
            if(v1[i][j]) coin=new MyCoin(":/Coin0001.png");
            else coin=new MyCoin(":/Coin0008.png");
            this->coinBtn[i][j]=coin;
            coin->setParent(this);
            coin->move(this->width()/2-98+i*pix_bgd.width(),202+j*pix_bgd.height());
            coin->posX=i;
            coin->posY=j;
            coin->flag=v1[i][j];//1代表金币  0代表银币
            connect(coin,&MyCoin::clicked,[=](){
                coinFlipSound->play();
                coin->changeFlag();                
                this->v1[i][j]=this->v1[i][j]==0?1:0;                
                if(coin->posX<3) this->coinBtn[i+1][j]->changeFlag();
                if(coin->posX>0) this->coinBtn[i-1][j]->changeFlag();
                if(coin->posY<3) this->coinBtn[i][j+1]->changeFlag();
                if(coin->posY>0) this->coinBtn[i][j-1]->changeFlag();
                this->isWin=true;
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        if(coinBtn[i][j]->flag==0){
                            this->isWin=false;
                            break;
                        }
                    }
                }
                if(this->isWin){
                    qDebug()<<"游戏胜利";
                    for(int i=0;i<4;i++){
                        for (int j=0;j<4;j++){
                            coinBtn[i][j]->isWin=true;
                        }
                    }
                    //播放胜利音乐，将预置好的胜利图片以动画的形式显示下来
                    succeedSound->play();
                    QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
                    animation->setDuration(1000);
                    animation->setStartValue(QRect((this->width()-pixWin.width())*0.5,-pixWin.height(),pixWin.width(),pixWin.height()));
                    animation->setEndValue(QRect((this->width()-pixWin.width())*0.5,pixWin.height(),pixWin.width(),pixWin.height()));
                    animation->setEasingCurve(QEasingCurve::OutElastic);
                    animation->start();
                }
            });

        }
    }
}

void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题图片
    pix.load(":/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
